package cn.jinjimi.app.webview;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import cn.jinjimi.app.R;
import cn.jinjimi.app.main.widget.biz.MenuConponentView;
import cn.jinjimi.app.main.widget.ToolBar.WebToolbar;

import java.util.List;

/**
 * DynamicMoreWindow
 * Date: 16/7/22
 * @author zhenhua
 */
public class DynamicMoreWindow extends PopupWindow implements View.OnClickListener {
    private Context mContext;
    private LinearLayout llAdd;

    public DynamicMoreWindow(Context context) {
        this.mContext = context;

        View view = View.inflate(context, R.layout.webview_customer_menu_layout, null);
        setContentView(view);

        llAdd = (LinearLayout) view.findViewById(R.id.ll_add);

        initListener(view);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        setFocusable(true);
        setTouchable(true);
        update();
    }

    public void setData(List<WebToolbar.ToolbarAction> actions, final View.OnClickListener listener) {

        llAdd.removeAllViews();

        if (actions == null) {
            return;
        }

        for (int i = 0; i < actions.size(); i++) {
            WebToolbar.ToolbarAction action = actions.get(i);

            View view = new MenuConponentView(mContext, action);
            view.setTag(action);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClick(v);
                    }
                    dismiss();
                }
            });

            if (llAdd != null) {
                llAdd.addView(view);
            }
        }
    }

    private void initListener(View view) {
        view.findViewById(R.id.more_menu_bg).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }
}
